package com.intelligence.controller;

import com.alibaba.fastjson2.JSON;
import com.intelligence.common.system.AuthContextUtil;
import com.intelligence.entity.pojo.SysUser;
import com.intelligence.entity.model.LoginModel;
import com.intelligence.entity.model.TokenModel;
import com.intelligence.entity.model.UserInfoModel;
import com.intelligence.entity.result.Result;
import com.intelligence.entity.result.ResultCodeEnum;
import com.intelligence.entity.vo.ValidateCodeVo;
import com.intelligence.service.SysUserService;
import com.intelligence.service.ValidateCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author : RulerWood
 * @Data : 2024/5/14 - 20:15
 */
@Slf4j
@RestController
@Tag(name = "用户模块")
@RequestMapping("/intelligence-user/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户登录
     *
     * @param loginModel 登录参数
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<TokenModel> loing(@RequestBody LoginModel loginModel) {
        log.info("login----->{}",loginModel);
        return sysUserService.login(loginModel);
    }

    /**
     *生成验证码
     * @return
     */
    @GetMapping(value = "/generateValidateCode")
    @Operation(summary = "生成验证码")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户登录后的信息
     * @param token
     * @return
     */
    @GetMapping(value = "/getUserInfo")
    @Operation(summary = "获取用户登录后的信息")
    public Result<UserInfoModel> getUserInfo(@RequestHeader(name = "token") String token) {
//        SysUser sysUser = sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.get();
        System.err.println("userinfo中的sysuser是--------》"+sysUser);


        log.info("getUserInfo---token-->{}",token);
//        String s = redisTemplate.opsForValue().get("user:login" + token);
//        SysUser sysUser = JSON.parseObject(s, SysUser.class);
        UserInfoModel userInfoModel = new UserInfoModel();
        userInfoModel.setUserName(sysUser.getUsername());
        userInfoModel.setAvatar(sysUser.getHeadimgurl());
        return Result.build(userInfoModel, ResultCodeEnum.SUCCESS);
    }

    /**
     * 登出
     * @param token
     * @return
     */
    @GetMapping(value = "/logout")
    @Operation(summary = "登出")
    public Result logout(@RequestHeader(value = "token") String token) {
        log.info("logout---token-->{}",token);
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
